package com.example.lists;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 根据对象的某个字段去重
 */
public class ListDistinct {
    public static void main(String[] args) {
        //取集合中某字段最大值的对象
        List<User> users = new ArrayList<>();
        users.add(new User(1, 1, "老王"));
        users.add(new User(2, 2, "老何"));
        users.add(new User(3, 3, "老王"));
        users.add(new User(4, 4, "老何"));
        users.add(new User(5, 5, "老赵"));
        List<User> distinctUsers = Optional.ofNullable(users).orElse(new ArrayList<>()).stream()
                .collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
                        new TreeSet<>(Comparator.comparing(u -> u.getLable()))), ArrayList::new));
        System.out.println(distinctUsers);
    }
}
